<?php

require_once ('php/model/place.php');

function combineDuplicatePlaces($places) {
    $combined_places = array();
    foreach ($places as $place1) {
        foreach ($places as $place2) {
            if ($place1->matches($place2)) {
                $place1->combineWith($place2);
                array_push($combined_places, $place1);
            }
        }
    }
    return array_unique($combined_places);
}

/**
 * This possibly a faster array unique, but it doesn't work for objects, 
 * only strings.
 * @param type $a
 * @return type
 */
function array_uniquer($a) {
    $n = array();
    foreach ($a as $k => $v)
        if (!in_array($v, $n))
            $n[$k] = $v;
    return $n;
}

function getCurrentURL() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function getIndexURL() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] == "localhost" ? 
                $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"])."/index.php" : 
                $_SERVER["SERVER_NAME"] . "/index.php";
    }
    return $pageURL;
}

function getLogoutURL() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] == "localhost" ? 
                $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"])."/logout.php" : 
                $_SERVER["SERVER_NAME"] . "/logout.php";
    }
    return $pageURL;
}

function getLogoutAjaxURL() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] == "localhost" ? 
                $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"])."/logout_ajax.php" : 
                $_SERVER["SERVER_NAME"] . "/logout_ajax.php";
    }
    return $pageURL;
}

function getURL($resource) {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] == "localhost" ? 
                $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"]).$resource : 
                $_SERVER["SERVER_NAME"] . $resource;
    }
    return $pageURL;
}

function getPublicClientIP() {
    if (isset($_SERVER["HTTP_CLIENT_IP"]))
    {
        return $_SERVER["HTTP_CLIENT_IP"];
    }
    elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
    {
        return $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
    {
        return $_SERVER["HTTP_X_FORWARDED"];
    }
    elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
    {
        return $_SERVER["HTTP_FORWARDED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_FORWARDED"]))
    {
        return $_SERVER["HTTP_FORWARDED"];
    }
    else
    {
        return $_SERVER["REMOTE_ADDR"];
    }
}
?>
